package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private ProvinceInfoService provinceInfoService;
    @Autowired
    private CityInfoService cityInfoService;
    @Autowired
    private DistrictInfoService districtInfoService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private FeeValueMapper feeValueMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Override
    public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //判断是更新还是新增，是否存在apartmentId
        Long apartmentId = apartmentSubmitVo.getId();
        //更新操作
        if(apartmentId != null){
            //删除配套信息
            QueryWrapper<ApartmentFacility> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("apartment_id", apartmentId);
            apartmentFacilityService.remove(queryWrapper1);

            //删除杂费信息
            QueryWrapper<ApartmentFeeValue> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("apartment_id", apartmentId);
            apartmentFeeValueService.remove(queryWrapper2);

            //删除标签信息
            QueryWrapper<ApartmentLabel> queryWrapper3 = new QueryWrapper<>();
            queryWrapper3.eq("apartment_id", apartmentId);
            apartmentLabelService.remove(queryWrapper3);

            //删除图片信息
            QueryWrapper<GraphInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("item_type", ItemType.APARTMENT).eq("item_id", apartmentId);
            graphInfoService.remove(queryWrapper);

        }

        //插入apartment信息
        Long provinceId = apartmentSubmitVo.getProvinceId();
        String provinceName = provinceInfoService.getById(provinceId).getName();
        apartmentSubmitVo.setProvinceName(provinceName);
        Long cityId = apartmentSubmitVo.getCityId();
        String cityName = cityInfoService.getById(cityId).getName();
        apartmentSubmitVo.setCityName(cityName);
        Long districtId = apartmentSubmitVo.getDistrictId();
        String districtName = districtInfoService.getById(districtId).getName();
        apartmentSubmitVo.setDistrictName(districtName);

        super.saveOrUpdate(apartmentSubmitVo);
        apartmentId = apartmentSubmitVo.getId();
        //插入配套信息
        List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        List<ApartmentFacility> apartmentFacilities = new ArrayList<>();
        for (Long facilityInfoId : facilityInfoIds) {
            ApartmentFacility apartment = ApartmentFacility.builder().build();
            apartment.setApartmentId(apartmentId);
            apartment.setFacilityId(facilityInfoId);
            apartmentFacilities.add(apartment);
        }
        apartmentFacilityService.saveBatch(apartmentFacilities);
        //插入杂费信息
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        List<ApartmentFeeValue> apartmentFeeValues = new ArrayList<>();
        feeValueIds.forEach(feeValueId -> {
            ApartmentFeeValue apartment = ApartmentFeeValue.builder().build();
            apartment.setApartmentId(apartmentSubmitVo.getId());
            apartment.setFeeValueId(feeValueId);
            apartmentFeeValues.add(apartment);
        });
        apartmentFeeValueService.saveBatch(apartmentFeeValues);
        //插入标签信息
        List<Long> labelIds = apartmentSubmitVo.getLabelIds();
        List<ApartmentLabel> apartmentLabels = new ArrayList<>();
        for (Long labelId : labelIds) {
            ApartmentLabel apartment = ApartmentLabel.builder().build();
            apartment.setApartmentId(apartmentId);
            apartment.setLabelId(labelId);
            apartmentLabels.add(apartment);
        }
        apartmentLabelService.saveBatch(apartmentLabels);
        //插入图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfos = new ArrayList<>();
        for (GraphVo graphVo : graphVoList) {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setItemType(ItemType.APARTMENT);
            graphInfo.setItemId(apartmentId);
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfos.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfos);
    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {

        return apartmentInfoMapper.pageApartmentItemByQuery(page, queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //查询公寓信息和对应的相关配置信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(id);
        BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);

        //查询公寓配套信息
        List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectListByApartmentId(id);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);

        //查询公寓标签信息
        List<LabelInfo> labelInfoList = labelInfoMapper.selectListByApartmentId(id);
        apartmentDetailVo.setLabelInfoList(labelInfoList);

        //查询公寓图片信息
        List<GraphInfo> graphInfoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);
        List<GraphVo> graphVoList = new ArrayList<>();
        for (GraphInfo graphInfo : graphInfoList) {
            GraphVo graphVo = new GraphVo();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            graphVoList.add(graphVo);
        }
        apartmentDetailVo.setGraphVoList(graphVoList);

        //查询公寓杂费信息
        List<FeeValueVo> feeValueList = feeValueMapper.selectListByApartmentId(id);
        apartmentDetailVo.setFeeValueVoList(feeValueList);
        return apartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long apartmentId) {
        //删除配套信息
        QueryWrapper<ApartmentFacility> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("apartment_id", apartmentId);
        apartmentFacilityService.remove(queryWrapper1);

        //删除杂费信息
        QueryWrapper<ApartmentFeeValue> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("apartment_id", apartmentId);
        apartmentFeeValueService.remove(queryWrapper2);

        //删除标签信息
        QueryWrapper<ApartmentLabel> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.eq("apartment_id", apartmentId);
        apartmentLabelService.remove(queryWrapper3);

        //删除图片信息
        QueryWrapper<GraphInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_type", ItemType.APARTMENT).eq("item_id", apartmentId);
        graphInfoService.remove(queryWrapper);

        //删除公寓房间信息
        QueryWrapper<RoomInfo> roomQueryWrapper = new QueryWrapper<>();
        roomQueryWrapper.eq("apartment_id", apartmentId);
        roomInfoMapper.delete(roomQueryWrapper);

        //删除公寓信息
        apartmentInfoMapper.deleteById(apartmentId);
    }
}




